angular.module('App').controller('DownloadFileController', ['$scope', '$http', 'BaseFunction', 
    function($scope, $http, BaseFunction) {
        $scope.Records;
        BaseFunction.request('/api/App/getSharedFileInfo', false, null, function(data){
            $scope.Records = data;
        });
        

        $scope.download = function(fileName){
            var aLink = document.createElement('a');
            aLink.download = fileName;
            aLink.href = '/smartplant/' + fileName;
            var evt = document.createEvent('MouseEvents');
            evt.initEvent("click", false, false);
            aLink.dispatchEvent(evt);
            URL.revokeObjectURL(aLink.href);

            return;

            //以下这种模式对于Chrome支持，但对于Android系统有的不兼容
            // var parameter = {
            //     FileName: fileName
            // };
            
            // $http.post('/api/App/getFileStream', parameter, {'responseType': 'arraybuffer'}).success(function (data, status, headers, config) {
            //     var blob = new Blob([data], {type: "application/octet-stream"});
            //     var aLink = document.createElement('a');
            //     aLink.download = fileName;
            //     aLink.href = URL.createObjectURL(blob);

            //     var evt = document.createEvent('MouseEvents');
            //     evt.initEvent("click", false, false);
            //     aLink.dispatchEvent(evt);
            //     URL.revokeObjectURL(aLink.href);
            // }).error(function (data, status, headers, config) {
            //     var newErr = {
            //         data: {
            //             leval: 5,
            //             title: '请求失败',
            //             message: data
            //         }
            //     };

            //     BaseFunction.errFun(newErr);
            // });
        };
    }
]);
